/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import Logica.Core.MateriaPrima.YerbaAjena;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;

/**
 *
 * @author Luciano
 */
public class YerbaAjenaJpaController implements Serializable {

    public YerbaAjenaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public YerbaAjenaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");

    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(YerbaAjena yerbaAjena) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(yerbaAjena);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findYerbaAjena(yerbaAjena.getId()) != null) {
                throw new PreexistingEntityException("YerbaAjena " + yerbaAjena + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(YerbaAjena yerbaAjena) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            yerbaAjena = em.merge(yerbaAjena);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = yerbaAjena.getId();
                if (findYerbaAjena(id) == null) {
                    throw new NonexistentEntityException("The yerbaAjena with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            YerbaAjena yerbaAjena;
            try {
                yerbaAjena = em.getReference(YerbaAjena.class, id);
                yerbaAjena.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The yerbaAjena with id " + id + " no longer exists.", enfe);
            }
            em.remove(yerbaAjena);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<YerbaAjena> findYerbaAjenaEntities() {
        return findYerbaAjenaEntities(true, -1, -1);
    }

    public List<YerbaAjena> findYerbaAjenaEntities(int maxResults, int firstResult) {
        return findYerbaAjenaEntities(false, maxResults, firstResult);
    }

    private List<YerbaAjena> findYerbaAjenaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from YerbaAjena as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public YerbaAjena findYerbaAjena(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(YerbaAjena.class, id);
        } finally {
            em.close();
        }
    }

    public int getYerbaAjenaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from YerbaAjena as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
